package com.twu.biblioteca;

import com.twu.biblioteca.models.Book;
import com.twu.biblioteca.models.Library;
import com.twu.biblioteca.models.Movie;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: mageshku
 * Date: 29/08/13
 * Time: 6:23 AM
 * To change this template use File | Settings | File Templates.
 */

public class LibraryInitializer{
    public static Library createSampleLibrary() throws IOException {
        String csvFile = "src/books.csv";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(csvFile));
        String line;
        Library library = new Library();
        while ((line = bufferedReader.readLine()) != null)
        {
            String[] csvBook = line.split(",");
            Book book = new Book(csvBook[0], Integer.parseInt(csvBook[1]));
            book.addAuthor(csvBook[2]);
            book.setPrice(Float.parseFloat(csvBook[3]));
            library.addBook(book);
        }

        csvFile = "src/movies.csv";
        bufferedReader = new BufferedReader(new FileReader(csvFile));
        while ((line = bufferedReader.readLine()) != null)
        {
            String[] csvMovie = line.split(",");
            Movie movie = new Movie(csvMovie[0]);
            movie.setDirector(csvMovie[1]);
            movie.setYear(Integer.parseInt(csvMovie[2].trim()));
            if (csvMovie.length > 3)
                movie.setRating(Float.parseFloat(csvMovie[3]));
            library.addMovie(movie);
        }
        return library;
    }

}
